package com.knife.bbs.business;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * 
 */
public class ActiveUserService implements HttpSessionAttributeListener {
	/*
	private static ActiveUserService singleton = new ActiveUserService();

	public static ActiveUserService getInstance() {
		return singleton;
	}
	*/

	private static final java.util.Map admins = new java.util.HashMap();
	private static final java.util.Map managers = new java.util.HashMap();
	private static final java.util.Map users = new java.util.HashMap();
	private static final java.util.Map guests = new java.util.HashMap();
	private long timeOut = 1000 * 60 * 10;// 定义最大超时时间
	private long refreshTime = 1000 * 60 * 2;// 定义刷新时间

	public void run() {
		while (true) {
			try {
				System.out.println("当前在线人数:" + getAllUserNum());
				Thread.sleep(refreshTime);
				refreshOnline(admins.values());
				refreshOnline(managers.values());
				refreshOnline(users.values());
				refreshOnline(guests.values());
			} catch (Exception e) {
				System.out.println("在线人数控制线程出现错误：" + e);
			}
		}
	}

	private void refreshOnline(java.util.Collection c) {
		java.util.Collection delC = new java.util.ArrayList();
		Iterator it = c.iterator();
		long now = System.currentTimeMillis();
		while (it.hasNext()) {
			ActiveUser user = (ActiveUser) it.next();
			if (now - user.getLastvisittime().getTime() > timeOut)
				delC.add(user);
		}
		c.removeAll(delC);
	}

	public static boolean isOnline(String userName) {
		return admins.containsKey(userName) || managers.containsKey(userName)
				|| users.containsKey(userName) || guests.containsKey(userName);
	}

	public ActiveUser getUser(String userName) {
		ActiveUser user = null;
		user = (ActiveUser) admins.get(userName);
		if (user == null)
			user = (ActiveUser) managers.get(userName);
		if (user == null)
			user = (ActiveUser) users.get(userName);
		if (user == null)
			user = (ActiveUser) guests.get(userName);
		return user;
	}

	public void join(ActiveUser user) {
		if (!isOnline(user.getUsername())) {
			BBSUserRole userRole = BBSUserRole.getUserRole(null, user);
			if (userRole.isAdministrator())
				admins.put(user.getUsername(), user);
			else if (userRole.isValidateUser())
				users.put(user.getUsername(), user);
			else if (userRole.isBbsManager())
				managers.put(user.getUsername(), user);
			else
				guests.put(user.getUsername(), user);
		}
	}

	public void remove(ActiveUser user) {
		if (user == null)
			return;
		this.admins.remove(user.getUsername());
		this.managers.remove(user.getUsername());
		this.users.remove(user.getUsername());
		this.guests.remove(user.getUsername());
	}

	public List getAdmins() {
		return new ArrayList(admins.values());
	}

	public List getManagers() {
		return new ArrayList(managers.values());
	}

	public List getUsers() {
		return new ArrayList(users.values());
	}

	public List getGuests() {
		return new ArrayList(guests.values());
	}

	public static List getAllUser() {
		java.util.List list = new ArrayList();
		list.addAll(admins.values());
		list.addAll(managers.values());
		list.addAll(users.values());
		list.addAll(guests.values());
		return list;
	}

	public static Integer getAllUserNum() {
		return new Integer(admins.size() + managers.size() + users.size()
				+ guests.size());
	}

	public static Integer getAdminNum() {
		return new Integer(admins.size());
	}

	public static Integer getManagerNum() {
		return new Integer(managers.size());
	}

	public static Integer getUsersNum() {
		return new Integer(users.size());
	}

	public static Integer getGuestNum() {
		return new Integer(guests.size());
	}

	public long getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(long refreshTime) {
		this.refreshTime = refreshTime;
	}

	public long getTimeOut() {
		return timeOut;
	}

	public void setTimeOut(long timeOut) {
		this.timeOut = timeOut;
	}

	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		if ("bbsuser".equals(arg0.getName())) {
			System.out.println("用户上线:"+arg0.getName()+"|"+arg0.getValue().toString());
			//list.add((String) arg0.getValue());
			//users.put((String) arg0.getValue(), arg0.getSession());
			this.join((ActiveUser)arg0.getValue());
		}
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		if ("bbsuser".equals(arg0.getName())) {
			//list.remove(arg0.getValue());
			this.remove((ActiveUser)arg0.getValue());
		}
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		//TODO Auto-generated method stub
	}

}
